package com.ddyx.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.ddyx.common.util.ImageIOUtil;
import com.ddyx.model.DeviceEventData;
import com.ddyx.model.FaceImageInfo;
import com.ddyx.service.IDeviceEventDataParser;

/***
 * 阅面设备事件解析器
 * 
 * @author zhongchzh
 *
 */
public class ReadSenceDeviceEventDataParser implements IDeviceEventDataParser {

	private static final Logger logger = LoggerFactory.getLogger(ReadSenceDeviceEventDataParser.class);
	
	public static final DateFormat dateformat_ymdhms = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
	
	@Override
	public DeviceEventData parse(JSONObject eventJson) {
		try {
			if(eventJson.containsKey("event")) {
				DeviceEventData result = new DeviceEventData();
				result.setReceivedTime(new Date());
				JSONObject eventObject = eventJson.getJSONObject("event");
				String device_id = eventObject.getString("device_name");
				result.setDeviceNo(device_id);
				result.setCapturedTime(dateformat_ymdhms.parse(eventObject.getString("capture_at")));
				String originalImageUrl = eventObject.getJSONObject("original_face").getString("url");
				result.setOriginalImageUrl(originalImageUrl);
				result.setOriginalImageData(ImageIOUtil.getImageBinary(originalImageUrl));
				FaceImageInfo faceImageInfo = new FaceImageInfo();
				faceImageInfo.setFaceImageUrl(originalImageUrl);
				faceImageInfo.setFaceImageData(result.getOriginalImageData());
				result.getFaceImages().add(faceImageInfo);
				return result;
			}
		} catch(Exception err) {
			logger.error("解析阅客设备事件数据错误。错误原因：", err);
		}
		return null;
	}

}
